package com.hnas.ecard.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnas.common.web.BaseAjaxResponse;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.dto.DataBean;
import com.hnas.ecard.service.MerchantService;
import com.hnas.ecard.service.dto.DTOMerchant;
import com.hnas.ecard.service.search.DTOMerchantSearch;

@Controller
@RequestMapping("/backend/merchant")
public class MerchantControl {
	
	private static Logger log = LoggerFactory.getLogger(MerchantControl.class);

	@Autowired
	private MerchantService merchantService;
	
	@RequestMapping("search")
	public String search(ModelMap model,HttpServletRequest request,DTOMerchantSearch con){
		try {
			DTOPagedList<DTOMerchant> pg = merchantService.search(con);
			model.put("dto", con);
			model.put("pg", pg);
		} catch (Exception e) {
			log.error("查询商户管理出现异常！");
			model.put("resultSize", 0);
			e.printStackTrace();
		}
		
		model.put("pageTitle", "商户管理");
		List<DataBean> beans = new ArrayList<DataBean>();
		beans.add(new DataBean("name", "name", "名称", false, 125));
		beans.add(new DataBean("pic", "pic", "图片", false, 125));
		beans.add(new DataBean("sort", "sort", "排序", false, 125));
		model.put("beans", beans);
		return "backend/ecard/listMerchant";
	}
	
	@RequestMapping(value="save",method=RequestMethod.POST)
	public String save(DTOMerchant dto,ModelMap model,String type,HttpServletRequest request){
		   model.put("type", type);
		try {
			merchantService.saveOrUpdate(dto,request);
			return "redirect:/backend/merchant/search";
		} catch (Exception e) {
			e.printStackTrace();
			log.error("保存商户信息异常:");
			model.put("msg", "保存商户信息异常,请联系管理员！");
			return "backend/ecard/showMerchant";
		}
	}
	
	@RequestMapping("showMerchant")
	public String showRegionInfo(ModelMap model,String id,String type){
		try {
			model.addAttribute("type", type);
			if(null!=id){
				DTOMerchant dTOMerchant = merchantService.findById(id);
				model.addAttribute("dto",dTOMerchant==null?new DTOMerchant():dTOMerchant);
				return "backend/ecard/showMerchant";
			}
			model.addAttribute("dto",new DTOMerchant());
			model.addAttribute("saveStr","");
			return "backend/ecard/showMerchant";
		} catch (Exception e) {
			e.printStackTrace();
			log.error("showMerchant查询异常");
			return "backend/modules/showMerchant";
		}
	}
	
	@RequestMapping("findById")
	@ResponseBody
	public BaseAjaxResponse findById(String id){
		BaseAjaxResponse res = new BaseAjaxResponse();
		try {
			if(null==id){
				res.setErrorMessage("ID 不能为空");
				res.setResult(BaseAjaxResponse.FAIL);;
				return res;
			}
			return res.retrunSuccess(res,merchantService.findById(id));
		} catch (Exception e) {
			e.printStackTrace();
			log.error("通过ID查询商家分类异常");
			return res.retrunFail(res);
		}
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public BaseAjaxResponse deleteById(String id){
		BaseAjaxResponse res = new BaseAjaxResponse();
		try {
			if(null==id){
				res.setErrorMessage("ID不能为空！");
				res.setResult(BaseAjaxResponse.FAIL);
				return res;
			}
			merchantService.deleteById(id);
			return res.retrunSuccess(res);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("通过id查找商家分类异常");
			return res.retrunFail(res);
		}
	}
}
